//
//  PopoverViewPresentationController.swift
//  mySwift
//
//  Created by 高赛 on 16/9/19.
//  Copyright © 2016年 高赛. All rights reserved.
//

import UIKit

class PopoverViewPresentationController: UIPresentationController {
    
    // 定义属性保存菜单的大小
    var presentFrame = CGRect.zero
    
    // 初始化方法  用于创建负责过渡动画的对象
    // presentedViewController: 被弹出来的控制器
    // presentingViewController: 发起的控制器
    override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
        
        super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
        
    }
    // 即将布局过渡动画的子视图时调用
    override func containerViewWillLayoutSubviews() {
        
        // 修改弹出视图的大小
//        containerView 容器视图
//        presentedView 被展现的视图
        if presentFrame == CGRect.zero {
            presentedView?.frame = CGRect(x: 100, y: 56, width: 200, height: 200)
        }else {
            presentedView?.frame = presentFrame
        }
        
        // 在容器视图上添加一个蒙版,插入到展现视图的下面
        // 因为展现视图和蒙版都在一个视图上,二后添加的会盖住先添加的
        containerView?.insertSubview(coverView, at: 0)
    }
    
    private lazy var coverView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        view.frame = UIScreen.main.bounds
        
        let tapGr = UITapGestureRecognizer(target: self, action: #selector(close))
        view.addGestureRecognizer(tapGr)
        return view
    }()
    
    func close() {
        presentedViewController.dismiss(animated: true, completion: nil)
    }
    
}
